﻿using System;
using System.Threading.Tasks;
using DailyDiet.Services;
using DailyDiet.Services.Constant;
using DailyDiet.util.eventArgs;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace DailyDiet.ViewModels
{
    /// <summary>
    /// 健康资讯ViewModel
    /// </summary>
    public class HealthInfoViewModel : ViewModelBase
    {

        /******** 构造函数 ********/
        private IPreferenceStorage _preference;
        private IDietStorageService _dietStorage;
        private IDietRecorderStorageService _dietRecorderStorage;
        private IFavoriteStorageService _favoriteStorage;
        private INotificationService _notificationService;
        private ISplitSentenceService _splitSentenceService;
        /// <summary>
        /// 健康信息服务
        /// </summary>
        private IHealthInfoService _healthInfoService;

        private IRecorderStorageFillMeassage _recorderStorageFillMeassage;

        /// <summary>
        ///健康资讯页的ViewModel
        /// </summary>
        /// <param name="healthInfoService">健康信息服务</param>
        public HealthInfoViewModel(
            IHealthInfoService healthInfoService, 
            IPreferenceStorage preferenceStorage,
            IDietStorageService dietStorageService,
            IFavoriteStorageService favoriteStorageService,
            IDietRecorderStorageService dietRecorderStorage,
            IRecorderStorageFillMeassage recorderStorageFillMeassage,
            INotificationService notificationService,
            ISplitSentenceService splitSentenceService)
        {
            _healthInfoService = healthInfoService;
            _preference = preferenceStorage;
            _dietRecorderStorage = dietRecorderStorage;
            _dietStorage = dietStorageService;
            _preference = preferenceStorage;
            _favoriteStorage = favoriteStorageService;
            _recorderStorageFillMeassage = recorderStorageFillMeassage;
            _dietRecorderStorage.DietRecorderStorageEvent += DietRecorderOnSet;
            _notificationService = notificationService;
            _splitSentenceService = splitSentenceService;
        }


        /******** 绑定属性 ********/

        /// <summary>
        /// 标题
        /// </summary>
        public string Title
        {
            get => _title;
            set => Set(nameof(Title), ref _title, value);
        }
        /// <summary>
        /// 标题
        /// </summary>
        private string _title;

        /// <summary>
        /// 描述
        /// </summary>
        public string Description
        {
            get => _description;
            set => Set(nameof(Description), ref _description, value);
        }
        /// <summary>
        /// 描述
        /// </summary>
        private string _description;

        /// <summary>
        /// 图片链接
        /// </summary>
        public string PicUrl
        {
            get => _picUrl;
            set => Set(nameof(PicUrl), ref _picUrl, value);
        }
        /// <summary>
        /// 图片链接
        /// </summary>
        private string _picUrl;

        /// <summary>
        /// 资讯链接
        /// </summary>
        public string Url
        {
            get => _url;
            set => Set(nameof(Url), ref _url, value);
        }
        /// <summary>
        /// 资讯链接
        /// </summary>
        private string _url;

        /******** 绑定命令 ********/

        /// <summary>
        /// 资讯展示命令
        /// </summary>
        private RelayCommand _exhibitCommand;
        public RelayCommand ExhibitCommand =>
            _exhibitCommand ?? (_exhibitCommand = new RelayCommand(async () => await ExhibitCommandFunc()));
        //监听DietRecorder事件，如果被提前设置就不再通知了
        private void DietRecorderOnSet(object sender,
            DietRecorderStorageEventArgs e)
        {
            String mealTimes = e.MealTimes;
            _notificationService.DeleteNotification(mealTimes);
        }

        public async Task ExhibitCommandFunc() {
            await Initialize();
        }

        /// <summary>
        /// 剥离的可测试函数 Initialize
        /// </summary>
        /// <returns></returns>
        public async Task Initialize() {
            var result = await _healthInfoService.GetHealthInfoAsync();
            Description = result.Description;
            PicUrl = result.PicUrl;
            Title = result.Title;
            Url = result.Url;
        }

        /// <summary>
        /// 资讯详情命令
        /// </summary>
        private RelayCommand _detailCommand;

        public RelayCommand DetailCommand =>
            _detailCommand ??
            (_detailCommand = new RelayCommand(async () =>
            {
                await DetailCommandFunction();
            }));
        public async Task DetailCommandFunction() =>
            await _healthInfoService.OpenAsync(Url);
    }
}
